package com.lenovo.testreport.user.service;

import com.lenovo.testreport.user.mapper.FeedbackMapper2;
import com.lenovo.testreport.user.mapper.UserinfoMapper2;
import com.lenovo.testreport.user.model.Feedback;
import com.lenovo.testreport.user.model.Userinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author CJF
 * @date 2018/10
 */
@Service("userservice")
public class UserServiceImpl implements UserService {

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @Autowired
    private UserinfoMapper2 UserMapper;
    @Autowired
    private FeedbackMapper2 QuestionMapper;

    @Override
    public Userinfo findUserByName(String name) {
        return UserMapper.selectByName(name);
    }

    @Override
    synchronized public int addUser(Userinfo userinfo) {
        String key = "AllUser";
        redisTemplate.delete(key);
        return UserMapper.insertSelective(userinfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    synchronized public int modifyUser(Userinfo user) {
        String key = "AllUser";
        redisTemplate.delete(key);
        return UserMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    synchronized public int addQuestion(Feedback question) {

        String key = "AllQuestion";
        redisTemplate.delete(key);

        return QuestionMapper.insert(question);
    }

    @Override
    synchronized public List<Userinfo> FindAllUser() {
        RedisSerializer redisSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(redisSerializer);
        String key = "AllUser";
        List<Userinfo> List = (List<Userinfo>) redisTemplate.opsForValue().get(key);
        if (List == null) {
            List = UserMapper.selectAll();
            redisTemplate.opsForValue().set(key, List, 120, TimeUnit.SECONDS);
            //  System.out.println(key+" 缓存中没找到，访问数据库！");
        }
        //  else System.out.println(key+" 缓存中找到了，无需访问数据库！");
        return List;
    }

    @Override
    synchronized public List<Feedback> finAllQuestion() {
        RedisSerializer redisSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(redisSerializer);
        String key = "AllQuestion";
        List<Feedback> List = (List<Feedback>) redisTemplate.opsForValue().get(key);
        if (List == null) {
            List = QuestionMapper.selectAll();
            redisTemplate.opsForValue().set(key, List, 120, TimeUnit.SECONDS);
            // System.out.println(key+ " 缓存中没找到，访问数据库！");
        }
        // else System.out.println(key+" 缓存中找到了，无需访问数据库！");
        return List;
    }

    @Override
    synchronized public Feedback FindQuestionById(int tid) {
        return QuestionMapper.selectByPrimaryKey(tid);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    synchronized public int updateQuestion(Feedback feedback) {
        int result = QuestionMapper.updateByPrimaryKeySelective(feedback);
        String key = "AllQuestion";
        redisTemplate.delete(key);
        return result;
    }

    @Override
    synchronized public List<Feedback> finQuestionByName(String name) {
        RedisSerializer redisSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(redisSerializer);
        String key = name + "'s question";
        List<Feedback> List = (List<Feedback>) redisTemplate.opsForValue().get(key);
        if (List == null) {
            List = QuestionMapper.selectByName(name);
            redisTemplate.opsForValue().set(key, List, 20, TimeUnit.SECONDS);
            // System.out.println(key+" 缓存中没找到，访问数据库！");
        }
        // else System.out.println(key+" 缓存中找到了，无需访问数据库！");
        return List;

    }

    @Override
    public Feedback findquestionByID(Integer integer) {
        return QuestionMapper.selectByPrimaryKey(integer);
    }

    @Override
    public Userinfo findUserById(int uid) {
        return UserMapper.selectByPrimaryKey(uid);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeUser(int uid) {
        int result = UserMapper.deleteByPrimaryKey(uid);
        String key = "AllUser";
        redisTemplate.delete(key);
        return result;
    }

    static public Integer ChangeLevelNum(int[] codes, int oldnum) {
        for (int code : codes
                ) {
            if (code >= 0) {
           /*    if(!(code==(code&oldnum))) {
                   oldnum+=code;
               }*/
                oldnum |= code;
            } else {
                code = -code;
              /*  if(code==(code&oldnum)) {
                    oldnum-=code;
                }*/
                oldnum = oldnum & ~code;
            }
        }
        return oldnum;
    }

}
